import Vue from 'vue';
import Vuex from 'vuex';
import test from './modules/demo/test';
import {login} from '@/apis/home';

Vue.use(Vuex);

// env里去获取当前的环境是否需要开启严格模式
// 在发布环境开启严格模式会造成性能上不必要的损失
// const debug = process.env.NODE_ENV !== 'production';

export default new Vuex.Store({
    modules: {
        // 测试模块
        test: Object.assign({}, {
            namespaced: true
        }, test)
    },
    state: {
        userInfo: {
            id: '',
            name: ''
        },
        isLogin: false
    },
    actions: {
        async login ({commit}, param) {
            let {data} = await login(param);
            let info = {id: data.id, name: data.name};
            window.sessionStorage.setItem('u-i', JSON.stringify(info));
            // window.localStorage.setItem('uid', JSON.stringify(info));
            commit('setUserInfo', info);
            commit('setIsLogin', true);
            return Promise.resolve(info);
        },
        logout ({commit}) {
            window.sessionStorage.removeItem('u-i');
            commit('setUserInfo', {id: '', name: ''});
            commit('setIsLogin', false);
        }
    },
    mutations: {
        setUserInfo (state, userInfo = {}) {
            state.userInfo = userInfo;
        },
        setIsLogin (state, b) {
            state.isLogin = b;
        }
    },
    getters: {
        userInfo: state => state.userInfo,
        isLogin: state => state.isLogin
    }
    // 是否开启严格模式
    // strict: debug
});
